package com.shu.driftbook.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.shu.driftbook.dao.Book;
import com.shu.driftbook.service.BookService;

public class BreifAction extends ActionSupport implements
ServletResponseAware,ServletRequestAware{
	private BookService service;
	private List<Book> tool,novel;
	private HttpServletResponse response;
	private HttpServletRequest request;
	public String execute(){
		service.setLimit(4);
		tool=service.findTool();
		novel=service.findNovel();
		service.setLimit(0);
		return SUCCESS;
	}
	public BookService getService() {
		return service;
	}
	public void setService(BookService service) {
		this.service = service;
	}
	public List<Book> getTool() {
		return tool;
	}
	public void setTool(List<Book> tool) {
		this.tool = tool;
	}
	public List<Book> getNovel() {
		return novel;
	}
	public void setNovel(List<Book> novel) {
		this.novel = novel;
	}
	public String FindCover() throws IOException{
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("bookname");
		name=URLDecoder.decode(name,"utf-8");
		Book book=service.find(name).get(0);
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store");  
        response.setHeader("Pragma", "no-cache");  
        response.setDateHeader("Expires", 0);
		PrintWriter out=response.getWriter();
		out.write(book.getCover());
		out.close();
		return SUCCESS;
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response=arg0;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request=arg0;
	}
}
